//
//  Contact_CusTableViewController.m
//  HuaRun2
//
//  Created by admin on 17/9/2.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Contact_CusTableViewController.h"
#import "TwoLabelView.h"
#import "Contact_RoleTableViewCell.h"
#import "Contact_ReportTableViewCell.h"
#import "Contact_ListModel.h"
#import "Contact_ReportModel.h"

@interface Contact_CusTableViewController ()

@end

@implementation Contact_CusTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    if (self.contact_CusListType == Contact_CusListTypeRole) {
//        "contact_detail_role"= "联系人角色列表";//
        self.navigationItem.title = NSLocalizedString(@"contact_detail_role", nil);
//        @"联系人角色";
        
    }else if (self.contact_CusListType == Contact_CusListTypeReport) {
//        "contact_detail_record"= "联系人记录";
        self.navigationItem.title = NSLocalizedString(@"contact_detail_record", nil);
//        @"联系人系统记录";
    }
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;

    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[Contact_RoleTableViewCell class] forCellReuseIdentifier:@"Contact_RoleTableViewCell"];
    [self.tableView registerClass:[Contact_ReportTableViewCell class] forCellReuseIdentifier:@"Contact_ReportTableViewCell"];

    self.tableView.backgroundColor = RGBA(249, 249, 249, 1);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    TwoLabelView * headerView = [TwoLabelView new];
    headerView.frame = CGRectMake(0, 0, kMainScreenWidth, 60);
    headerView.nameLab.text = [NSString showSpaceStringWithStr:_name];
    headerView.companyLab.text = [NSString showSpaceStringWithStr:_companyName];
    
    self.tableView.tableHeaderView = headerView;

    [self methodBind];
}

- (void)methodBind{
//    if (self.contact_CusListType == Contact_CusListTypeReport) {
//        [self fetchContactReportList];
//    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.contact_CusListType == Contact_CusListTypeRole) {
        return _dataArray.count;
    }else if (self.contact_CusListType == Contact_CusListTypeReport) {
        return _dataArray.count;
    }
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.contact_CusListType == Contact_CusListTypeRole) {
        
        Contact_RoleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Contact_RoleTableViewCell" forIndexPath:indexPath];
        cell.roleModel = _dataArray[indexPath.row];
        
        return cell;
    }else if (self.contact_CusListType == Contact_CusListTypeReport) {
        Contact_ReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Contact_ReportTableViewCell" forIndexPath:indexPath];
        cell.contact_ReportModel = _dataArray[indexPath.row];
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"1";
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.contact_CusListType == Contact_CusListTypeRole) {
        return 150;
    }else if (self.contact_CusListType == Contact_CusListTypeReport) {
        return 130;
    }
    return 44;
}
//获取联系人系统记录
- (void)fetchContactReportList {
    NSString * url = [NSString stringWithFormat:@"%@%@", kBaseUrl,
                      kContact_Detail_Report_ListUrl];
//    NSString * accountId = self.personModel ? (self.personModel.id ?
//                                               self.personModel.id : @"" ) : @"";
    NSString * accountId = _contactId ? _contactId : @"";
    NSDictionary * dic = @{@"contactId": accountId};
    
    weakself(ws);
    [NetWorkHelper getWithURLString:url parameters:dic success:^(id object) {
        if (![object[@"code"] isEqual: kSuccessCode]) {
            NSString * info = [NSString isemptyStr:object[@"msg"]] ? kUnknownError : object[@"msg"];
            [ws showWithText:info];
            return;
        }
        
        NSArray * addressArray = [NSArray yy_modelArrayWithClass:[Contact_ReportModel class]
                                                            json:object[@"list"]];
        ws.dataArray = addressArray;
        [ws.tableView reloadData];
        
    } failure:^(NSError *error) {
        [ws showWithText:kNetworkError];
    }];
}
@end
